/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haipv.gtkhoa501.screen;

import com.haipv.gtkhoa501.controller.Event;
import com.haipv.gtkhoa501.controller.StorageData;
import com.haipv.gtkhoa501.controller.TimerManager;
import com.haipv.gtkhoa501.model.AnswerCode;
import com.haipv.gtkhoa501.model.Exam;
import com.haipv.gtkhoa501.model.Question;
import com.haipv.gtkhoa501.model.Var;
import com.haipv.gtkhoa501.util.IObsever;
import com.haipv.gtkhoa501.util.Util;
import com.nokia.lwuit.components.HeaderBar;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.html.HTMLComponent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.GenericListCellRenderer;
import java.io.IOException;
import java.util.Hashtable;

/**
 *
 * @author PHANHAI Giao diện hiển thị làm bài tập
 */
public class ExamScreen extends BaseScreen implements ActionListener, IObsever {

    String colorFirst = "<span style=\"color:#49A530\">";
    String colorLast = " </span>";
    String xuongDong = "</br></br>";
    int index = 1;
    Command back = new Command(Var.back);
    Exam question;
    List list;
    Label lblTime;
    TimerManager timer;

    public ExamScreen(int index) {
        controller.SetFullScreen();
        Var.tap = 2;
        timer = TimerManager.getInstance();
        if (controller.categoryBarExam.getVisibility() == false) {
            controller.categoryBarExam.setVisibility(true);
        }
        this.index = index;
        question = storage.getExam(index);


        //Reaet câu người dùng đã chọn
        for (int i = 0; i < 4; i++) {
            Var.testDapAn[i] = (AnswerCode.checkAnswer(question.getAnswer(), i + 1));
        }
        timer.addObsever(this);
    }

    public void showForm() {


        Form form = new Form();
        form.setScrollableY(false);
        form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        Container contai = new Container(new BorderLayout());
        Label lab = new Label();
        lab.setText(Var.cau + index + "/30");
        lab.getStyle().setFgColor(Var.colorHearder);

        contai.addComponent(BorderLayout.WEST, lab);
        //  anh add them cai gì đó vào container contai này để làm cái thời gian
        if (lblTime == null) {
            lblTime = new Label(timer.getCurrent());
            lblTime.getStyle().setFgColor(Var.colorHearder);
            contai.addComponent(BorderLayout.EAST, lblTime);
        }
        //  cái time đếm ngược đc add vào container đó nhe
        contai.setScrollable(false);
        form.addComponent(contai);

        Container con = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        Container con1 = new Container();

        HTMLComponent htmlComp = new HTMLComponent(null);
        htmlComp.setBodyText("   " + question.getQuestion().getContent() + xuongDong, "UTF-8");
        con1.addComponent(htmlComp);
        con1.getStyle().setMargin(5, 15, 5, 5);
        con.addComponent(con1);

        Container con2 = new Container(new BorderLayout());
        if (question.getQuestion().getImg().length() != 0) {
            try {
                int so = Integer.parseInt(question.getQuestion().getImg());
                Label lb = null;
                lb = new Label(Image.createImage("/images/" + so + ".jpg"));
                lb.getStyle().setAlignment(Label.CENTER);
                //lb.getStyle().setMargin(10, 10, 5, 5);
                con2.getStyle().setAlignment(Container.CENTER);
                con2.addComponent(BorderLayout.CENTER, lb);

                con.addComponent(con2);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        list = new List(createGenericListCellRendererModelData());
        Container c = createGenericRendererContainer();
        Container c1 = createGenericRendererContainer();
        list.setRenderer(new GenericListCellRenderer(c, c1));
        list.addActionListener(this);

        con.addComponent(list);

        con.setScrollableY(true);
        form.addComponent(con);
        form.setBackCommand(back);
        form.addCommand(back);

        form.addCommandListener(this);
        form.show();
    }

    private Container createGenericRendererContainer() {
        Container f = new Container(new BoxLayout(BoxLayout.X_AXIS));
        f.getStyle().setBgTransparency(0);
        CheckBox selected = new CheckBox();
        selected.setName("Seclect");
        selected.setPreferredW(36);
        selected.setFocusable(true);
        Container e = new Container(new BorderLayout());
        Container d = new Container();
        e.getStyle().setBgTransparency(0);
        d.getStyle().setBgTransparency(0);

        e.addComponent(BorderLayout.CENTER, selected);
        //Font font = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        TextArea textArea = new TextArea("Name", 3, 2);
        textArea.getStyle().setFgColor(0x000000);

        textArea.setName("Name");
        textArea.setFocusable(true);
        textArea.setEditable(false);

        d.addComponent(textArea);
        f.addComponent(e);
        f.addComponent(d);
        return f;
    }

    private Hashtable[] createGenericListCellRendererModelData() {
        Hashtable[] data = new Hashtable[question.getQuestion().getOptions().size()];
        //  cài đặt truyền dữ liệu vào câu trả lời
        String checked;
        for (int i = 0; i < question.getQuestion().getOptions().size(); i++) {
            data[i] = new Hashtable();
            checked = (AnswerCode.checkAnswer(question.getAnswer(), i + 1)) ? "true" : "false";
            data[i].put("Seclect", checked);
            data[i].put("Name", question.getQuestion().getOptions().elementAt(i).toString());
        }
        return data;
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getCommand() == back) {
            timer.stop();
            controller.handleEvent(Event.MAIN_ACTION, null);
        } else if (ae.getSource() == list) {
            if (Var.testDapAn[list.getSelectedIndex()] == true) {
                Var.testDapAn[list.getSelectedIndex()] = false;
            } else {
                Var.testDapAn[list.getSelectedIndex()] = true;
            }
            question.setAnswer(AnswerCode.getCode(
                    Var.testDapAn[0], Var.testDapAn[1], Var.testDapAn[2], Var.testDapAn[3]));
        }
    }

    public void updateTime(String time) {
        lblTime.setText(time);
    }

    public synchronized void finish() {
        timer.stop();
        if (Var.tap != 1) {
            controller.handleEvent(Event.EXAM_FINISH, null);
        }
    }
}
